package jnpf.form.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.base.controller.SuperController;
import jnpf.constant.MsgCode;
import jnpf.form.entity.AppFormDataEntity;
import jnpf.form.entity.LeaveApplyEntity;
import jnpf.form.service.AppFormDataService;
import jnpf.form.service.LeaveApplyService;
import jnpf.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 请假申请
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月27日 上午9:18
 */
@Slf4j
@Tag(name = "请假申请", description = "LeaveApply")
@RestController
@RequestMapping("/api/workflow/Form/data")
public class SystemFormDataController extends SuperController<LeaveApplyService, LeaveApplyEntity> {

    @Autowired
    private AppFormDataService appFormDataService;

    /**
     * 获取请假申请信息
     *
     * @param id 主键值
     * @return
     */
    @Operation(summary = "获取系统参数信息")
    @GetMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult<Map<String,Object>> info(@PathVariable("id") String id) {
        AppFormDataEntity appFormDataEntity = appFormDataService.getById(id);
        log.info("查询表单数据---------------------------->{}", JSONObject.toJSONString(appFormDataEntity.getObjectData()));
        JSONObject formJson = JSONObject.parseObject(appFormDataEntity.getObjectData());
        if (formJson.containsKey("formOperates")) {
            formJson.remove("formOperates");
        }
        Map<String, Object> map = JsonUtil.entityToMap(formJson);
        log.info("表单map数据为---------------------------->{}",JSONObject.toJSONString(map));
        return ActionResult.success(map);
    }

    /**
     * 新建请假申请
     *
     * @param
     * @return
     */
    @Operation(summary = "新建请假申请")
    @PostMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult create(@PathVariable("id") String id, @RequestBody JSONObject data) {
        log.info("id---------------------------->{}",id);
        log.info("表单数据---------------------------->{}",data.toJSONString());
        AppFormDataEntity appFormDataEntity = new AppFormDataEntity();
        appFormDataEntity.setId(id);
        appFormDataEntity.setApp(data.getString("app"));
        appFormDataEntity.setBusinessType(data.getString("businessType"));
        appFormDataEntity.setObjectData(data.toJSONString());
        appFormDataService.save(appFormDataEntity);
        return ActionResult.success(MsgCode.SU006.get());
    }

    /**
     * 修改请假申请
     *
     * @param id             主键
     * @return
     */
    @Operation(summary = "修改请假申请")
    @PutMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult update(@PathVariable("id") String id) {
        return ActionResult.success(MsgCode.SU006.get());
    }
}
